package com.luo.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author Zoro
 * @date 2021年11月02
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        // 初始化CORS配置对象
        CorsConfiguration config = new CorsConfiguration();
        // 允许的域，不要写*，否则cookie不可用
        config.addAllowedOrigin("http://manager.gmall.com");
        config.addAllowedOrigin("http://www.gmall.com");
        config.addAllowedOrigin("http://item.gmall.com");
        // 允许的头信息
        config.addAllowedHeader("*");
        // 语序的请求方式
        config.addAllowedMethod("*");
        // 是否语序携带cookie信息
        config.setAllowCredentials(true);
        //添加映射路径，我们拦截一切请求
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(corsConfigurationSource);


    }
}
